<!DOCTYPE html>
<html lang="zh">

	<head>
        <base href="{:config('site_url')}/" />

		<meta charset="utf-8">
		<meta name="viewport" content="target-densitydpi=device-dpi,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<meta name="keywords" content="{$seo.keywords|default=" "}" />
		<meta name="description" content="{$seo.description|default=" "}" />
		<meta name="author" content="{$cfg.owner|default=" itboye "}" />
      <!-- 系统基本样式 -->
        <link type="text/css" rel="stylesheet" href="__CDN__/bootstrap/3.3.7/css/bootstrap.min.css" />


      <!-- 脚本 -->
		<script type="text/javascript" src="__CDN__/jquery/1.11.0/jquery.min.js"></script>
		<script type="text/javascript" src="__CDN__/bootstrap/3.3.7/js/bootstrap.min.js"></script>

		{block name="area_header"}
      <!--顶部区域 -->
		{/block}
		<title>网速测试</title>


        <style type="text/css">
            .admin-main {
              margin-top: 0px;
            }
            div{
              font-size: 14px;
              font-weight: bold;
            }
          .j_result {
            text-align: center;
          }
          .j_result .title {
              font-size: 18px;
          }
          .help{
            text-align: center;
            color: #666666;
          }
        </style>
	</head>

	<body class="theme-simplex">


      <div class="j_result result">
        <div class="title">测试到森森服务器的网络速度</div>
        <div class="help">1秒 = 1000毫秒</div>
        <div class="help">
          您的IP: {$ip}.请确保是在家庭Wifi环境下打开<br/>请联系客服, QQ群: 452705030</div>
          <div class="total">
          </div>
          <div class="delay">
          </div>
      </div>

      <div class="j_net_tester">
        <div class="tip">请稍后</div>
        <div class="progress">
          <div class="progress-bar"  role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">
            0%
          </div>
        </div>
      </div>

    <div class="text-center">
      <a href="{:url('')}" class="j_refresh btn btn-primary" style="display: none">重新测速</a>
    </div>



    <script type="text/javascript">

          var netResult = {
              'total': 0, //总次数
              'success': 0, //成功次数
              'fail': 0, //失败次数
              'max_delay': 0, //最大延时
              'min_delay': 0,//最小延时
              'every_delay': [

              ], //每次耗时记录
              'avg_delay': function () {
                  let sum = 0;
                  let trueCnt = 0;
                  for (let i = 0; i < this.every_delay.length; i++) {
                      if (this.every_delay[i][2] > 0) {
                        trueCnt++;
                        sum += (this.every_delay[i][2]);
                      }
                  }
                  if (trueCnt === 0) return 0;
                  return (sum / trueCnt).toFixed(0);
              },
              'suc_percent': function () {
                  if (this.total === 0) return 0;
                  return this.success / this.total;
              }
          };
      var netTester = (function ($, netResult) {

          function every(i, cnt) {
              let percent = (((i / cnt) * 100).toFixed(2)) + "%";
              $(".j_net_tester div.progress-bar").css("width", percent).text(percent);
              show();
              if (i === cnt) {
                  return ;
              }

              window.netResult.every_delay[i][0] = new Date().getTime();
              $.ajaxSetup({
                  async : true
              });
              $.get(netTester.url + "?id=" + i).done(function (data){
                  console.log(this, window.netResult);
                  window.netResult.success++;
              }).fail(function () {
                  // console.log('fail');
                  window.netResult.fail++;
              }).always(function () {
                  let endTimestamp = new Date().getTime();
                  window.netResult.every_delay[i][1] = endTimestamp;
                  let delay = endTimestamp - window.netResult.every_delay[i][0];
                  window.netResult.every_delay[i][2] = delay;
                  if (delay > window.netResult.max_delay) {
                      window.netResult.max_delay = delay;
                  }
                  if (window.netResult.min_delay === 0 || delay < window.netResult.min_delay) {
                      window.netResult.min_delay = delay;
                  }
                  every(i+1, cnt);
              });

          }

          function start($ele, cnt) {
              console.log('net tester start');

              netResult.total = cnt;
              for (let j=0; j < cnt; j++) {
                  netResult.every_delay[j] = [0,0,0];
              }

              netResult =  every(0, cnt, netResult);
              console.log('result', netResult);
              return netResult;
          }


          return {
              url : "http://www.baidu.com",
              start: start
          };

      })($, netResult);

      function show() {
          console.log("*************结果*********************");
          console.log("总请求次数:" + netResult.total);
          console.log("成功次数:" + netResult.success, "失败次数" + netResult.fail);
          console.log("平均延时:" + netResult.avg_delay() + "毫秒");
          console.log("最小延时: " + netResult.min_delay + "毫秒", "最大延时" + netResult.max_delay + "毫秒");
          console.log("*************************************");
          $(".j_result .total").html("请求成功: " + netResult.success + "<br/>" +  "请求失败: " + netResult.fail);
          $(".j_result .delay").html("<br/>" +  "最小延时: " + netResult.min_delay + "毫秒" + "<br/>" +  "最大延时: " + netResult.max_delay + "毫秒"+"<br/>" + "平均延时: " + netResult.avg_delay() + "毫秒");
          $(".j_refresh").show();
      }
      $(function () {
          netTester.url = "http://test.sunsunxiaoli.com:8888/web.php/net/test.html?id=18";
          var i = 3;
          function ready() {
              if (i === 0) {
                  $(".j_net_tester .tip").hide();
                  $(".j_net_tester .progress").show();
                  netTester.start($(".j_net_tester"), 20);
                  return;
              }

              $(".j_net_tester .tip").text((i--) + "s 后自动进行测试");
              setTimeout(ready, 1000);
          }
         setTimeout(ready, 1000);
      })
    </script>
    </body>

</html>